var path = require('path'),
    webpack = require('webpack'),
    config = require('./config'),
    srcDir = path.resolve(process.cwd(), 'src'),
    imagePath = path.resolve(srcDir, 'img'),
    cssPath = path.resolve(srcDir, 'css'),
    jsPath = path.resolve(srcDir, 'js'),
    viewsPath = path.resolve(srcDir, 'views'),
    nodeModulesPath = path.resolve(__dirname, 'node_modules'),
    autoprefixer = require('autoprefixer'),
    ExtractTextPlugin = require("extract-text-webpack-plugin"),
    publicPath = config.domain + '/',
    HappyPack = require('happypack'),
    HtmlWebpackPlugin = require('html-webpack-plugin'),
    CopyWebpackPlugin = require('copy-webpack-plugin'),
    CleanPlugin = require('clean-webpack-plugin'),
    os = require('os'),
    happyThreadPool = HappyPack.ThreadPool({
        size: os.cpus().length
    });
let webpackConfig = {
    cache: true,
    entry: {
        index: path.join(__dirname, "src/views/index.jsx")
    },
    root: srcDir,
    output: {
        publicPath: publicPath,
        path: path.join(__dirname, "build/static/"), //文件输出目录
        filename: "core/[name].[hash:5].js", //根据入口文件输出的对应多个文件名
        chunkFilename: "core/[name].[chunkhash:5].js"
    },
    resolve: {
        extensions: ['', '.js', '.jsx', '.json'], // 现在你require文件的时候可以直接使用require('file')，不用使用require('file.jsx')
        alias: { //配置别名，在项目中可缩减引用路径
            'img': imagePath,
            'css': cssPath,
            'js': jsPath,
            'views': viewsPath
        },
        modulesDirectories: ["node_modules"]
    },
    module: {
        noParse: [/jquery/],
        loaders: [{
            test: /\.(png|jpg|gif)$/,
            include: [imagePath],
            loader: "url-loader?limit=1000&name=img/[name].[hash:5].[ext]"
        }, {
            test: /\.css$/,
            loader: ExtractTextPlugin.extract('style', 'css', 'postcss')
        }, {
            test: /\.scss$/,
            loaders: ["style-loader", "css-loader", 'postcss-loader', "sass-loader"],
        }, {
            test: /\.jsx$/,
            exclude: [nodeModulesPath],
            include: [srcDir],
            loader: "babel-loader",
            query: {
                presets: ['react', 'es2015'],
                "plugins": [
                    ["import", {
                        "libraryName": "antd-mobile",
                        "style": "css"
                    }]
                ]
            }
        }, {
            test: /\.js$/,
            exclude: [nodeModulesPath],
            include: [srcDir],
            loader: "babel-loader",
            query: {
                presets: ['es2015']
            }
        }]
    },
    postcss: [autoprefixer],
    sassLoader: {
        imagePath: imagePath,
        includePaths: [cssPath],
    },
    plugins: [
        new HappyPack({
            id: 'happybabel',
            loaders: ['babel-loader'],
            threadPool: happyThreadPool,
            cache: true,
            verbose: true
        }),
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': '"production"'
        }),
        new webpack.DllReferencePlugin({ // 加快webpack打包速度
            context: __dirname,
            manifest: require('./build/static/vendor-manifest.json')
        }),
        new CopyWebpackPlugin([{
            from: path.resolve(__dirname, './src/js/lib'),
            to: path.resolve(__dirname, './build/static/lib')
        }]),
        new ExtractTextPlugin('css/[name].[contenthash:5].css'),
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: '"production"',
            },
        }),
        new webpack.optimize.UglifyJsPlugin({
            output: {
                comments: false,
            },
            compress: {
                warnings: false
            }
        }),
        new HtmlWebpackPlugin({
            filename: path.resolve(__dirname, './build/html/index.html'),
            template: path.resolve(__dirname, './build/html/index_dll.html'),
            inject: true
        })
    ]
};

if (process.env.clean) {
    webpackConfig.plugins.push(new CleanPlugin(['core'], {
        root: path.join(__dirname, "build/static"),
        verbose: true,
        dry: false
    }));
    webpackConfig.plugins.push(new CleanPlugin(['img'], {
        root: path.join(__dirname, "build/static"),
        verbose: true,
        dry: false
    }));
}

module.exports = webpackConfig;